import 'dart:convert';

import 'package:animate_do/animate_do.dart';
import 'package:animated_text_kit/animated_text_kit.dart';
import 'package:flutter/material.dart';
import 'package:flutter_lib/utils/LogUtils.dart';
import 'package:flutter_my_web/bean/GetHomeDataResponse.dart';
import 'package:flutter_my_web/logic/HomeGetLogic.dart';
import 'package:flutter_lib/utils/DpUtils.dart';
import 'package:flutter_my_web/widget/CardRadiusWidget.dart';
import 'package:flutter_my_web/widget/ImageWidget.dart';
import 'package:flutter_my_web/widget/TextWidget.dart';
import 'package:flutter_swiper_null_safety/flutter_swiper_null_safety.dart';
import 'package:get/get.dart';

class HomeTopWidget extends StatefulWidget {
  var mHomeGetLogic = Get.find<HomeGetLogic>().obs;

  @override
  _HomeTopWidgetState createState() => _HomeTopWidgetState();
}

class _HomeTopWidgetState extends State<HomeTopWidget> {
  late AnimationController animateController;

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Material(
      child: Container(
        width: MediaQuery.of(context).size.width,
        height: MediaQuery.of(context).size.height,
        child: Stack(
          children: [
            Container(
              width: MediaQuery.of(context).size.width,
              height: MediaQuery.of(context).size.height,
              //第一屏背景gif
              child: ImageLoadUtils(
                url: widget.mHomeGetLogic.value.jsonHomeDataResponse.topdata!.bg??"",
              ),
            ),
            Container(
              width: MediaQuery.of(context).size.width,
              height: MediaQuery.of(context).size.height,
              color: Color(0x33000000),
            ),
            Center(
                child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                (widget.mHomeGetLogic.value.jsonHomeDataResponse.topdata!.banner!.length >
                            0)
                    ? TopSwipeLayout(widget.mHomeGetLogic.value.jsonHomeDataResponse.topdata!.banner!)
                    : Container(),
                Container(
                  height: 10.0.hDp,
                ),
                ZuoYouMengWidget(widget.mHomeGetLogic.value.jsonHomeDataResponse.topdata!.textTitle,
                    widget.mHomeGetLogic.value.jsonHomeDataResponse.topdata!.context),
              ],
            )),
            Align(
                alignment: Alignment.bottomCenter,
                child: Container(
                    height: 30.0.wDp,
                    width: 30.0.wDp,
                    margin: EdgeInsets.all(30.0.hDp),
                    child: InkWell(
                      onTap: () {
                        widget.mHomeGetLogic.value.mScrollController
                            .jumpTo(100);
                      },
                      child: ElasticInDown(
                        from: 100,
                        to: 0,
                        controller: (controller) {
                          animateController = controller;
                          animateController.addStatusListener((status) {
                            if (status == AnimationStatus.completed) {
                              animateController.reset();
                              animateController.forward();
                            }
                          });
                        },
                        duration: Duration(seconds: 1),
                        child: Icon(
                          Icons.download_rounded,
                          color: Colors.white,
                          size: 30.0.wDp,
                        ),
                      ),
                    ))),
          ],
        ),
      ),
    );
  }
}

class TopSwipeLayout extends StatelessWidget {
  List<GetHomeJSONDataBanner> banner;

  TopSwipeLayout(this.banner);

  Widget _swiperBuilder(BuildContext context, int index) {
    return Stack(
      children: [
        (banner[index] != null && banner[index].bg!.isNotEmpty)
            ? Container(
          width: double.infinity,
              child: ImageLoadUtils(
                  url: banner[index].bg,
          fit: BoxFit.fitWidth,
                ),
            )
            : Container(),
        Positioned(
          bottom: 30.0.hDp,
            left: 0,
            right: 0,
            child: Container(
              padding: EdgeInsets.all(10.0.wDp),
              color: Color(0x33000000),
              child: Center(
                child: SimpleText(
          banner[index].title ?? "",
          fontSize: 20.sp,
          color: Colors.white,
        ),
              ),
            )),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: MediaQuery.of(context).size.width / 2,
      child: AspectRatio(
        aspectRatio: 16 / 9,
        child: Swiper(
          itemBuilder: _swiperBuilder,
          itemCount: banner.length,
          pagination: new SwiperPagination(
              builder: DotSwiperPaginationBuilder(
            color: Colors.black54,
            activeColor: Colors.white,
          )),
          // control: new SwiperControl(),
          scrollDirection: Axis.horizontal,
          autoplay: banner.length>1,
          onTap: (index){
            // JumpActionUtils.jumpAction(banner[index].title??"",
            //     banner[index].link??"", banner[index].type??"");
          },
        ),
      ),
    );
  }
}

class ZuoYouMengWidget extends StatelessWidget {
  String? title;
  String? message;

  ZuoYouMengWidget(this.title, this.message);

  @override
  Widget build(BuildContext context) {
    return Container(
      width: MediaQuery.of(context).size.width / 1.3,
      // height: 100.0.hDp,
      child: ShapeRadiusWidget(
        circular: 10,
        bgColor: Colors.transparent,
        margin: EdgeInsets.all(0),
        padding: EdgeInsets.all(10.0.wDp),
        child: Center(
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              GestureDetector(
                onTap: (){
                  // FileManagerApi().getImageTitleTimeList().then((value){
                  //   LogUtils.e(json.encode(value.data.titleTimes));
                  // });
                },
                child: SimpleText(
                  title ?? "",
                  fontSize: 16.sp,
                  color: Colors.white,
                ),
              ),
              AnimatedTextKit(
                animatedTexts: [
                  TypewriterAnimatedText(
                    message ??
                        "",
                    textStyle: TextStyle(
                      color: Colors.white,
                      fontSize: 10.sp,
                    ),
                    speed: Duration(milliseconds: 50),
                  ),
                ],
                isRepeatingAnimation: true,
                totalRepeatCount: 99999,
                pause: Duration(seconds: 5),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
